<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\DeportmentRequest;
use App\Services\Api\ApiService;
use App\Services\DeportmentService;
use App\Http\Controllers\Controller;

class DeportmentController extends Controller
{
    private $service;

    public function __construct(ApiService $apiService, DeportmentService $service)
    {
        parent::__construct($apiService);
        $this->service = $service;
    }

    /**
     * @return mixed
     */
    public function all(){
        $this->service->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria'));
        $datas = $this->service->repository->scopeQuery(function ($query){
            return $query->with('patientType', 'doctor');
        })->all();
        return $this->apiService->run(200, $datas);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $showPage = $this->getShowPage();
        if (!empty(request()->input('showpage'))){
            $showPage = request()->input('showpage');
        }
        $this->service->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria'));
        $datas = $this->service->repository->scopeQuery(function ($query){
            return $query->with('patientType', 'doctor');
        })->paginate($showPage);
        return $this->apiService->run(200, $datas);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(DeportmentRequest $request)
    {
        try{
            $data = $this->service->repository->create($request->all());
            return $this->apiService->run(200, $data);
        }catch (\Exception $exception){
            return $this->apiService->run(500);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $data = $this->service->repository->scopeQuery(function ($query){
            return $query->with('patientType', 'doctor');
        })->find($id);
        //if (empty($patient)) return $this->apiService->run(400);
        return $this->apiService->run(200, $data);
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(DeportmentRequest $request, $id)
    {
        try {
            $data = $this->service->repository->find($id)->update($request->all());
            if (empty($data)) return $this->apiService->run(400);
            return $this->apiService->run(200);
        }catch (\Exception $exception){
            return $this->apiService->run(500);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = $this->service->repository->delete($id);
        if (empty($data)) return $this->apiService->run(400);
        return $this->apiService->run(200);
    }
}
